package cn.iocoder.yudao.module.member.api.user;

import cn.iocoder.yudao.module.member.api.user.dto.MemberUserPriceDetailRespDTO;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserPriceRespDTO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserPriceConvert;
import cn.iocoder.yudao.module.member.convert.user.MemberUserPriceDetailConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserPriceDetailDo;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserPriceDo;
import cn.iocoder.yudao.module.member.service.user.MemberUserPriceDetailService;
import cn.iocoder.yudao.module.member.service.user.MemberUserPriceService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Validated
public class MemberUserPriceApiImpl implements MemberUserPriceApi{

    @Resource
    private MemberUserPriceService memberUserPriceService;
    @Resource
    private MemberUserPriceDetailService memberUserPriceDetailService;
    /**
     * 基于用户昵称，模糊匹配用户列表
     *
     * @param userId 用户昵称，模糊匹配
     * @return 用户信息的列表
     */
    @Override
    public List<MemberUserPriceRespDTO> getUserPrices(Long userId) {
        List<MemberUserPriceDo> userByUserId = memberUserPriceService.getUserByUserId(userId);
        List<MemberUserPriceRespDTO> memberUserPriceRespDTOS = MemberUserPriceConvert.INSTANCE.convertList2(userByUserId);
        //查询明细配置信息
        List<MemberUserPriceDetailDo> userByUserDetails = memberUserPriceDetailService.getUserByUserId(userId);
        Map<String, List<MemberUserPriceDetailDo>> collect = userByUserDetails.stream().collect(Collectors.groupingBy(MemberUserPriceDetailDo::getPriceCode));
        memberUserPriceRespDTOS.stream().forEach(t->{
            if (collect.containsKey(t.getPriceCode())){
                List<MemberUserPriceDetailDo> memberUserPriceDetailDos = collect.get(t.getPriceCode());
                List<MemberUserPriceDetailRespDTO> memberUserPriceDetailRespDTOS = MemberUserPriceDetailConvert.INSTANCE.convertList2(memberUserPriceDetailDos);
                t.setDetails(memberUserPriceDetailRespDTOS);
            }
        });
        return memberUserPriceRespDTOS;
    }
}
